#!/usr/bin/python3
# 2016.11.16 read or write file with utf-8


class FileReader:
    def __init__(self, name):
        self.name = name
        self.f = None

    def __enter__(self):
        self.f = open(self.name, encoding='utf-8', mode='rt')
        return self.f

    def __exit__(self, exc_type, exc_val, exc_tb):
        if self.f:  # 如果你enter过了，那么if就是True的，就可以关闭了
            self.f.close()


class FileWriter:
    def __init__(self, name, mode='w'):
        self.name = name
        self.f = None
        self.mode = mode

    def __enter__(self):
        self.f = open(self.name, encoding='utf-8', mode=self.mode)
        return self.f

    def __exit__(self, exc_type, exc_val, exc_tb):
        if self.f:  # 如果你enter过了，那么if就是True的，就可以关闭了
            self.f.close()


if __name__ == '__main__':
    with FileWriter("E:/test.txt", 'x') as w:
        with FileReader('E:/激活码.txt') as f:
            for line in f:
                w.write(line)
        # i = 0
        # for line in f.readlines():
        #     i += 1
        #     print(line)
        #     if i > 10:
        #         break

